const axios = require('axios');

const BASE_URL = 'http://localhost:5000/api';

// 测试数据
const testData = {
  suggestionId: 'test_suggestion_123',
  actions: [
    'set_reminder',
    'mark_preparing', 
    'reschedule_overdue',
    'elevate_priority',
    'end_work_today',
    'plan_tomorrow',
    'adjust_date',
    'remind_later',
    'merge_drink_reminders',
    'set_10min_reminder'
  ]
};

let authToken = '';
let userId = '';

async function testSuggestionFeedback() {
  console.log('🧪 开始测试AI建议反馈功能...\n');
  
  // 第一步：登录获取token
  console.log('🔐 正在登录获取认证token...');
  try {
    const loginResponse = await axios.post(`${BASE_URL}/auth/login/email`, {
      email: 'admin@aide.com',
      password: 'admin123456'
    });
    
    if (loginResponse.data.success) {
      authToken = loginResponse.data.token;
      userId = loginResponse.data.user.id;
      console.log('✅ 登录成功');
      console.log(`   用户ID: ${userId}`);
      console.log(`   Token: ${authToken.substring(0, 20)}...`);
    } else {
      console.log('❌ 登录失败:', loginResponse.data);
      return;
    }
  } catch (error) {
    console.log('❌ 登录错误:', error.response?.data || error.message);
    return;
  }
  
  console.log('\n📝 开始测试AI建议动作...\n');
  
  for (const action of testData.actions) {
    try {
      console.log(`📝 测试动作: ${action}`);
      
      const response = await axios.post(`${BASE_URL}/ai/suggestion-feedback`, {
        suggestionId: testData.suggestionId,
        action: action,
        rating: 5,
        feedbackText: `测试动作: ${action}`
      }, {
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${authToken}`
        }
      });
      
      if (response.data.success) {
        console.log(`✅ ${action} - 成功`);
        console.log(`   响应: ${JSON.stringify(response.data, null, 2)}`);
      } else {
        console.log(`❌ ${action} - 失败`);
        console.log(`   错误: ${JSON.stringify(response.data, null, 2)}`);
      }
      
         } catch (error) {
       console.log(`❌ ${action} - 错误`);
       if (error.response) {
         console.log(`   状态码: ${error.response.status}`);
         console.log(`   错误信息: ${JSON.stringify(error.response.data, null, 2)}`);
       } else {
         console.log(`   网络错误: ${error.message}`);
         console.log(`   错误详情: ${error.code || 'unknown'}`);
         console.log(`   错误堆栈: ${error.stack ? error.stack.split('\n')[0] : 'no stack'}`);
       }
     }
    
    console.log(''); // 空行分隔
  }
  
  console.log('🎉 测试完成！');
}

// 运行测试
testSuggestionFeedback().catch(console.error);
